﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;
using Model;
using Common;
namespace UI
{
    public partial class empower : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request["auth_code"] == null)
            {
                string code = ConfigDAL.GetPreAthCode();
                Response.Redirect("https://mp.weixin.qq.com/safe/bindcomponent?action=bindcomponent&auth_type=3&no_scan=1&component_appid=wxef2c55ebada8928b&pre_auth_code=" + code + "&redirect_uri=http://www.kdxny84.cn/empower.aspx&auth_type=1#wechat_redirect");
            }
            else
            {
                string autocode = Request["auth_code"];
                dynamic apiauth = ConfigDAL.GetAPIAuth(autocode);
                dynamic obj = apiauth.authorization_info;
                string authorizer_appid = obj.authorizer_appid;
                string authorizer_access_token = obj.authorizer_access_token;
                string authorizer_refresh_token = obj.authorizer_refresh_token;
                dynamic autoinfo = ConfigDAL.GetAPIAuthInfo(authorizer_appid);

                if (!BusinessDAL.IsExist(autoinfo.authorization_info.authorizer_appid))
                {
                    BusinessInfo info = new BusinessInfo()
                    {
                        ID = 0,
                        NickName = autoinfo.authorizer_info.nick_name,
                        HeadImg = autoinfo.authorizer_info.head_img,
                        ServiceTypeInfo = SerializeHelper.ToJsonByDynamic(autoinfo.authorizer_info.service_type_info),
                        VerifyTypeInfo = SerializeHelper.ToJsonByDynamic(autoinfo.authorizer_info.verify_type_info),
                        UserName = autoinfo.authorizer_info.user_name,
                        PrincipalName = autoinfo.authorizer_info.principal_name,
                        Alias = autoinfo.authorizer_info.alias,
                        BusinessInfos = SerializeHelper.ToJsonByDynamic(autoinfo.authorizer_info.business_info),
                        QrcodeUrl = autoinfo.authorizer_info.qrcode_url,
                        AuthorizationInfo = SerializeHelper.ToJsonByDynamic(autoinfo.authorization_info),
                        AuthorizationAppID = autoinfo.authorization_info.authorizer_appid,
                        FuncInfo = SerializeHelper.ToJsonByDynamic(autoinfo.authorization_info.func_info),
                        AuthorizerAccessToken = authorizer_access_token,
                        AuthorizerRefreshToken = authorizer_refresh_token,
                        CreateDate = DateTime.Now
                    };
                    int ID = BusinessDAL.Save(info);
                }
            }
        }
    }
}